<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: %LICENSE%%%
// $Author: vshoor $ ($Date: 2006/03/20 23:42:59 $)
// $RCSfile: functions_mail.php,v $ : $Revision: 1.15 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!class_exists("htmlMimeMail"))
{
	require_once ("./includes/Mail/htmlMimeMail.php");
}

/*
* Class for sending, decoding and storing mail data
*/
class SWIFT_Mail extends htmlMimeMail
{
	/**
	* Constructor
	*/
	function SWIFT_Mail()
	{
		global $_SWIFT;

		$this->setHeader('X-Mailer', SWIFTPRODUCT ." v".SWIFTVERSION);
		$this->setHeader('X-Priority', "3");
		$this->setHeader('MIME-Version', '1.0');

		$this->setHeader('Date', date('r'));

		if ($_SWIFT["settings"]["cpu_enablesmtp"] == 1)
		{
			$this->crlf = "\n";
		} else {
			$this->crlf = "\r\n";
		}
	}

	/**
	* Validate the Email Address
	*/
	function isValidEmail($email) {
		return (eregi( 
			'^[-!#$%&\'*+\\./0-9=?A-Z^_`{|}~]+'.      // the user name 
			'@'.                                      // the ubiquitous at-sign 
			'([-0-9A-Z]+\.)+' .                       // host, sub-, and domain names 
			'([0-9A-Z]){2,5}$',                       // top-level domain (TLD) 
			trim($email))); 
	}

	/**
	* Sets the data
	*/
	function setData($html, $text, $dohtml = false)
	{
		global $_SWIFT;

		if ($_SWIFT["settings"]["cpu_enablehtmlmails"] == 1 || $dohtml == true)
		{
			$this->setHtml($html, $text);
		} else {
			$this->setText($text);
		}
	}

	/**
	* Sends a mail, adds to mail queue if required. Attachments are not supported if you use mail queue
	*/
	function sendMail($toemail, $fromemail, $fromname = false, $usequeue = false)
	{
		global $_SWIFT, $dbCore, $settings;

		if (!is_array($toemail))
		{
			$email = array($toemail);
		} else {
			$email = $toemail;
		}

		// Check to see if mail queue is enabled
		if ($_SWIFT["settings"]["cpu_enablemailqueue"] != 1)
		{
			$usequeue = false;
		}

		// Set the required encoding and charsets if needed
		if ($_SWIFT["language"]["html_encoding"] != "")
		{
			$this->build_params['html_encoding'] = $_SWIFT["language"]["html_encoding"];
			$this->build_params['text_encoding'] = $_SWIFT["language"]["text_encoding"];
			$this->build_params['html_charset']  = $_SWIFT["language"]["html_charset"];
			$this->build_params['text_charset']  = $_SWIFT["language"]["text_charset"];
			$this->build_params['head_charset']  = $_SWIFT["language"]["text_charset"];
		}

		// Check to see if we need to use smtp
		if ($_SWIFT["settings"]["cpu_enablesmtp"] == 1)
		{
			$mailtype = "smtp";

			$this->smtp_params['host'] = $_SWIFT["settings"]["cpu_smtphost"];
			$this->smtp_params['port'] = $_SWIFT["settings"]["cpu_smtpport"];
			$this->smtp_params['helo'] = $_SWIFT["settings"]["cpu_smtphost"];
			$this->smtp_params['auth'] = iif($_SWIFT["settings"]["cpu_smtpuseauth"]==1, true, false);
			$this->smtp_params['user'] = $_SWIFT["settings"]["cpu_smtpuser"];
			$this->smtp_params['pass'] = $_SWIFT["settings"]["cpu_smtppass"];
		} else {
			$mailtype = "mail";
		}

		if (!$usequeue)
		{
			if ($fromname != "")
			{
				$from = "\"". $fromname ."\" <". $fromemail .">";
			} else {
				$from = $fromemail;
			}

			$this->setFrom($from);
			$this->setReturnPath($fromemail);
			$this->setHeader("Reply-To", $fromemail);
			$this->setHeader("X-Mailer", "HC Jsc ".SWIFTPRODUCT ." v".SWIFTVERSION);

			foreach ($email as $key=>$val)
			{
				$this->send(array($val), $mailtype);
			}
		} else {
			$mailsql = array();

			foreach ($email as $key=>$val)
			{
				$mailsql[] = "('". $dbCore->escape($val) ."', '". $dbCore->escape($fromemail) ."', '". $dbCore->escape($fromname) ."', '". $dbCore->escape($this->headers['Subject']) ."', '". $dbCore->escape(str_replace("[\$email]", $val, iif(empty($this->html_text), $this->text, $this->html_text))) ."', '". $dbCore->escape(str_replace("[\$email]", $val, $this->html)) ."', '". DATENOW ."', '". iif(empty($this->html), "0", "1") ."')";

				if (count($mailsql) > 50)
				{
					$dbCore->shutdownQuery("INSERT INTO `". TABLE_PREFIX ."mailqueuedata` (`toemail`, `fromemail`, `fromname`, `subject`, `datatext`, `datahtml`, `dateline`, `ishtml`) VALUES ". implode(",", $mailsql).";");
					unset($mailsql);
				}
			}

			if (count($mailsql))
			{
				$dbCore->shutdownQuery("INSERT INTO `". TABLE_PREFIX ."mailqueuedata` (`toemail`, `fromemail`, `fromname`, `subject`, `datatext`, `datahtml`, `dateline`, `ishtml`) VALUES ". 	implode(",", $mailsql).";");
				unset($mailsql);
			}

			$_queuecount = $settings->getKey("mail", "queuecount");
			if (empty($_queuecount))
			{
				$result = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."mailqueuedata`;");
				$settings->updateKey("mail", "queuecount", $result["totalitems"], true);
			}

			if (!defined("USESHUTDOWNFUNC"))
			{
				processMailQueue();
			} else {
				registerShutdown("mail");
			}
		}
	}
}

?>